package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.mapper.IClientMapper;
import com.project.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private IClientMapper clientMapper;

    /**
     * 分页动态查询
     *
     * @param pageNo   页码
     * @param name     客户名称
     * @param business 经营范围
     * @return 分页集合对象
     */
    @Override
    public Page<ClientBean> findByItem(Integer pageNo, String name, String business) {
        QueryWrapper<ClientBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("c_name", name);
        }
        if (business != null && business.length() != 0) {
            qw.like("c_business", business);
        }
        return clientMapper.selectPage(new Page<>(pageNo, 3), qw);
    }

    /**
     * 按id查看客户信息
     *
     * @param id 客户编号
     * @return 客服对象
     */
    @Override
    public ClientBean findById(Integer id) {
        return clientMapper.selectById(id);
    }

    /**
     * 按id修改客户信息
     *
     * @param id       客户id
     * @param user     负责人
     * @param phone    负责人电话
     * @param business 经营范围
     */
    @Override
    public void update(Integer id, String user, String phone, String business) {
        ClientBean client = clientMapper.selectById(id);
        client.setUser(user);
        client.setPhone(phone);
        client.setBusiness(business);
        clientMapper.updateById(client);
    }

    /**959
     * 添加客户对象
     *
     * @param client 客户对象
     */
    @Override
    public void add(ClientBean client) {
        clientMapper.insert(client);
    }

    /**
     * 查询所有客户
     *
     * @return 客户集合
     */
    @Override
    public List<ClientBean> findAll() {
        return clientMapper.selectList(null);
    }
}
